package com.gomore.opple.module.gooddetail.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.gomore.opple.OppleApplication;
import com.gomore.opple.R;
import com.gomore.opple.utils.DensityUtil;

import java.util.List;

/**
 * Created by asus on 2017/5/15.
 */
public class MyGridViewAdapter extends BaseAdapter {
    Context mContext;
    List<String> mListData;

    public MyGridViewAdapter(Context context, List<String> listData) {
        this.mContext = context;
        this.mListData = listData;
    }

    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mListData == null ? 0 : mListData.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_grid_view_item, parent, false);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        RequestListener<String, GlideDrawable> listener = new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                Log.d("132123", "onException: " + e.toString() + "  model:" + model + " isFirstResource: " + isFirstResource);
                viewHolder.imageView.setImageResource(R.mipmap.goods_place);
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                Log.e("132123", "isFromMemoryCache:" + isFromMemoryCache + "  model:" + model + " isFirstResource: " + isFirstResource);
                return false;
            }
        };

        Glide.with(mContext)
                .load(mListData.get(position))
                .asBitmap()
                .skipMemoryCache(true)
                .placeholder(R.mipmap.goods_place)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        int imageWidth = resource.getWidth();
                        int imageHeight = resource.getHeight();
                        int height = DensityUtil.screenWith(mContext) * imageHeight / imageWidth;
                        ViewGroup.LayoutParams para = viewHolder.imageView.getLayoutParams();
                        para.height = height;
                        para.width = DensityUtil.screenHeight(mContext);
                        viewHolder.imageView.setImageBitmap(resource);
                    }
                });
        return convertView;
    }

    private static class ViewHolder {
        ImageView imageView;
    }
}
